<?php
include '../controllers/QuestionController.php';
$page = $_REQUEST ['page'];
$count = 10;
$questions = getAllQuestions ( $page, $count );
$totalCount = getCountOfQuestion ();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="../js/question.js"></script>
</head>
<body>
	<a href='../index.php'>返回首页</a>
	<h1>问题墙</h1>
	<hr />
	<?php
	$questionCount = count ( $questions );
	for($i = 0; $i < $questionCount; $i ++) {
		$question = $questions [$i];
		$formatTime = date ( "y-m-d h:i:s", $question->timestamp );
		$answers = $question->getAnswers ();
		?>
		<table border='2' rules='rows' cellpadding='8%' align='center'
		width='80%'>
		<tr>
			<th width='20%'>问题标题</th>
			<td><?php echo $question->title;?></td>
		</tr>

		<tr>
			<th>问题详情</th>
			<td><?php echo $question->content;?></td>
		</tr>

		<tr>
			<th>提问时间</th>
			<td><?php echo $formatTime;?></td>
		</tr>

		<tr>
			<td colspan=2>
				<table border='2' rules='all' cellpadding='8%' align='center'
					width='95%'>
					<?php
		$answerCount = count ( $answers );
		for($j = 0; $j < $answerCount; $j ++) {
			$answer = $answers [$j];
			?>
					<tr style='background-color: gray'>
						<td width='20%'>第<?php echo $answerCount - $j;?>条回答</td>
						<td><input type="button" value="删除回答"
							onclick="deleteAnswer(<?php echo $answer->answerId;?>);"></input></td>
					</tr>
					<tr>
						<th>回答老师</th>
						<td><?php echo $answer->nickname;?></td>
					</tr>
					<tr>
						<th>老师说明</th>
						<td><?php echo $answer->introduce;?></td>
					</tr>
					<tr>
						<th>回答内容</th>
						<td><?php echo $answer->content;?></td>
					</tr>
					<tr>
						<th>回答时间</th>
						<td><?php 
							$formatAnswerTime = date ( "y-m-d h:i:s", $answer->timestamp );
							echo $formatAnswerTime;?>
							</td>
					</tr>
							<?php
		}
		?>
				</table>
			</td>
		</tr>

		<tr>
			<td><a
				href="answerquestion.php?questionId=<?php echo $question->questionId;?>">添加答案</a></td>
			<td><input type="button" value="删除问题"
				onclick="deleteQuestion(<?php echo $question->questionId;?>);"></input></td>

		</tr>

	</table>
	<br />
	<br />
	<?php
	}
	$totalPage = ceil ( $totalCount / $count );
	if ($totalPage <= 1) {
		exit ();
	} else if ($totalPage == $page) {
		?><div align="center">
		<a href="questionlist.php?page=<?php echo $page-1;?>">上一页</a>
		 <?php echo $page;?>/<?php echo $totalPage;?>页
		 <?php
	} else if ($totalPage > 1) {
		?>
		<a href="questionlist.php?page=<?php echo $page-1;?>">上一页</a>
		<?php echo $page;?>/<?php echo $totalPage;?>页
		<a href="questionlist.php?page=<?php echo $page+1;?>">下一页</a>
	</div>
	<?php
	
}
	?>
</body>
</html>